package Offer.offer2016;

import domain.ListNode;

import java.util.Stack;

public class Test05 {

    public static void main(String[] args) {
        ListNode ln = new ListNode(1);
        ListNode p = ln;
        p.next = new ListNode(2);
        p = p.next;
        p.next = new ListNode(3);
        p = p.next;
        p.next = new ListNode(4);
        p = p.next;
        stackPrint(ln);
    }

    public static void print(ListNode head) {
        Stack st = new Stack();
        while (head != null) {
            st.push(head.val);
            head = head.next;
        }
        while (!st.isEmpty()) {
            System.out.println(st.pop());
        }

    }

    public static void stackPrint(ListNode head) {
        Stack<ListNode> stack = new Stack<ListNode>();
        while (head != null) {
            stack.push(head);
            head = head.next;
        }
        while (!stack.isEmpty()) {
            ListNode tmpNode = stack.pop();
            System.out.print(tmpNode.val + " ");
        }
    }
}
